package com.manor.main.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.manor.common.Constants;
import com.manor.common.adapter.RefreshAdapter;
import com.manor.common.glide.ImgLoader;
import com.manor.common.utils.StringUtil;
import com.manor.common.utils.WordUtil;
import com.manor.main.R;
import com.manor.main.bean.EachFollowBean;

import java.util.List;

/**
 * Created by cxf on 2018/9/29.
 */

public class EachFollowAdapter extends RefreshAdapter<EachFollowBean> {

    private View.OnClickListener mClickListener;
    private View.OnClickListener mFollowClickListner;
    private ActionListenerX mActionListener;
    private String mIdString;
    private String mFanString;
    private String mFollowAddString;
    private String mFollowCancelString;
    private Drawable mFollowAddDrawable;
    private Drawable mFollowCancelDrawable;
    private int mFollowAddColor;
    private int mFollowCancelColor;


    public EachFollowAdapter(Context context) {
        super(context);
        mFollowAddString = WordUtil.getString(R.string.follow_each);
        mFollowCancelString = WordUtil.getString(R.string.follow_cancel);
        mFollowAddDrawable = ContextCompat.getDrawable(context, R.drawable.btn_follow_1_x);
        mFollowCancelDrawable = ContextCompat.getDrawable(context, R.drawable.btn_follow_0_x);
//        mFollowAddColor = ContextCompat.getColor(context, R.color.global);
//        mFollowCancelColor = 0xffb3b3b3;
        mFollowAddColor = ContextCompat.getColor(context, R.color.white);
        mFollowCancelColor = ContextCompat.getColor(context, R.color.white);
        mClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!canClick()) {
                    return;
                }
                Object tag = v.getTag();
                if (tag == null) {
                    return;
                }
                EachFollowBean bean = (EachFollowBean) tag;
                if (mActionListener != null) {
                    mActionListener.onItemClick(bean);
                }
            }
        };
        mFollowClickListner = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!canClick()) {
                    return;
                }
                Object tag = v.getTag();
                if (tag == null) {
                    return;
                }
                EachFollowBean bean = (EachFollowBean) tag;
                if (mActionListener != null) {
                    mActionListener.onFollowClick(bean);
                }
            }
        };
        mIdString = WordUtil.getString(R.string.serial_id);
        mFanString = WordUtil.getString(R.string.search_fans);
    }



    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Vh(mInflater.inflate(R.layout.item_follow_each, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder vh, int position) {

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder vh, int position, @NonNull List payloads) {
        Object payload = payloads.size() > 0 ? payloads.get(0) : null;
        ((Vh) vh).setData(mList.get(position), payload);
    }

    public void updateItem(String id, int attention) {
        Logger.e("updateItem id=="+id+",attention=="+attention);
        if (TextUtils.isEmpty(id)) {
            return;
        }
        for (int i = 0, size = mList.size(); i < size; i++) {
            EachFollowBean bean = mList.get(i);
            if (bean != null && id.equals(bean.getUid())) {
                bean.setAttent(attention);
                bean.setNeedFollow(attention!=1);
                notifyItemChanged(i, Constants.PAYLOAD);
                break;
            }
        }
    }


    class Vh extends RecyclerView.ViewHolder {

        ImageView mAvatar;
        TextView mName;
        ImageView mLevelAnchor;
        TextView mID;
        TextView mFans;
        TextView mBtnFollow;
        View mVip;

        public Vh(View itemView) {
            super(itemView);
            mAvatar = itemView.findViewById(R.id.avatar);
            mName = itemView.findViewById(R.id.name);
            mLevelAnchor = itemView.findViewById(R.id.level_anchor);
            mID = itemView.findViewById(R.id.id_val);
            mFans = itemView.findViewById(R.id.fans);
            mBtnFollow = itemView.findViewById(R.id.btn_follow);
            mVip = itemView.findViewById(R.id.vip);
            itemView.setOnClickListener(mClickListener);
            mBtnFollow.setOnClickListener(mFollowClickListner);
        }

        void setData(EachFollowBean bean, Object payload) {
            itemView.setTag(bean);
            mBtnFollow.setTag(bean);
            if (payload == null) {
                ImgLoader.displayAvatar(mContext, bean.getAvatar(), mAvatar);
                mName.setText(bean.getUser_nickname());
//                LevelBean levelBean = CommonAppConfig.getInstance().getAnchorLevel(bean.getLevelAnchor());
//                if (levelBean != null) {
//                    ImgLoader.display(mContext, levelBean.getThumb(), mLevelAnchor);
//                }
                mID.setText(StringUtil.contact(mIdString, bean.getSerial_no()));
//                mFans.setText(StringUtil.contact(mFanString, StringUtil.toWan(bean.getFans())));
            }
            if (!bean.isNeedFollow()) {
                mBtnFollow.setText(mFollowCancelString);
                mBtnFollow.setBackground(mFollowCancelDrawable);
                mBtnFollow.setTextColor(mFollowCancelColor);
            } else {
                mBtnFollow.setText(mFollowAddString);
                mBtnFollow.setBackground(mFollowAddDrawable);
                mBtnFollow.setTextColor(mFollowAddColor);
            }
//            if (bean.isVip()) {
//                if (mVip.getVisibility() != View.VISIBLE) {
//                    mVip.setVisibility(View.VISIBLE);
//                }
//            } else {
//                if (mVip.getVisibility() == View.VISIBLE) {
//                    mVip.setVisibility(View.INVISIBLE);
//                }
//            }
        }
    }


    public interface ActionListenerX {
        void onItemClick(EachFollowBean userBean);

        void onFollowClick(EachFollowBean userBean);
    }


    public void setActionListener(ActionListenerX actionListener) {
        mActionListener = actionListener;
    }
}
